package com.ctim.web.user;

import com.ctim.common.exception.EnumPcsService;
import com.ctim.common.exception.PcsRunTimeException;
import com.ctim.common.pcs.PcsResult;
import com.ctim.model.user.Resource;
import com.ctim.model.user.User;
import com.ctim.service.user.ResourceService;
import com.ctim.web.common.BaseController;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * Author: Wangwei
 * Date: 2016/9/9
 * Desc:
 */
@RestController
@RequestMapping("/rest/resource")
public class ResourceController extends BaseController {

    @Autowired
    private ResourceService resourceService;


    @RequestMapping(value = "/permission",method = RequestMethod.GET)
    public PcsResult permission(@RequestParam("parentId") Integer parentId) {
        Assert.notNull(parentId,"父级id不能为空");
        return newResult().setData(resourceService.getPermission(parentId));
    }

    @RequestMapping(value = "/menus",method = RequestMethod.GET)
    public PcsResult menuTree() {
        return newResult().setData(resourceService.getMenuTree());
    }


    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public PcsResult resourceList() {
        List<Resource> list = resourceService.getResources();
        return newResult().setData(list);
    }

    /*
    @GET
    @Path("treelist")
    @Produces(MediaType.APPLICATION_JSON)
    public PcsResult resourceTreeList(@QueryParam("roleId") Integer roleId) {
        List<Resource> list = resourceService.getResourcesTree(roleId);
        return newResult().setData(list);
    }

    @GET
    @Path("menu")
    @Produces(MediaType.APPLICATION_JSON)
    public PcsResult menuList() {
        List<Resource> list = resourceService.getMenuResources();
        return newResult().setData(JSONUtil.filterIncludeProperties(list, Resource.class,
                "id", "text", "type", "url", "iconCls", "iconCls", "parentId", "status", "leaf", "children"));
    }

    @GET
    @Path("button")
    @Produces(MediaType.APPLICATION_JSON)
    public PcsResult buttonList(@QueryParam("resourceId")Integer resourceId) {
        Assert.notNull(resourceId,"resourceId不能为空");
        List<Resource> list = resourceService.getButtonResources(resourceId);
        return newResult().setData(list);
    }*/

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public PcsResult insert( Resource resource) {
        User user = getUser();
        return resourceService.insertResource(resource,user)?newResult().setData(resource):failedResult(EnumPcsService.ERROR_OPERATE);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public PcsResult delete(@RequestParam("resourceId") Integer resourceId) {
        Assert.notNull(resourceId,"资源id不能为空");
        return resourceService.deleteResource(resourceId)?newResult():failedResult(EnumPcsService.ERROR_OPERATE);
    }

    @RequestMapping(value = "/toggleEnable",method = RequestMethod.POST)
    public PcsResult toggleEnable(@RequestParam("resourceId") Integer resourceId, @RequestParam("status") Integer status) {
        Assert.notNull(resourceId,"资源id不能为空");
        Assert.notNull(status,"资源状态不能为空");
        if (status!=0 && status!=1) {
            throw new PcsRunTimeException("资源状态不合法");
        }
        return resourceService.toggleEnable(resourceId, getUserId(),status)?newResult():failedResult(EnumPcsService.ERROR_OPERATE);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public PcsResult update( Resource resource) {
        Assert.notNull(resource.getId(),"资源id不能为空");
        return resourceService.updateResource(resource,getUser())?newResult():failedResult(EnumPcsService.ERROR_OPERATE);
    }

}
